<template>
  <el-dialog
    :visible="visible"
    :before-close="handleClose"
    :close="handleClose"
    :show-close="true"
    :class="{'global-mobile-dialog': showMobilePage}"
    class="small-common-dialog-size add-dialog"
  >
    <Title title="添加账号" style="margin-bottom:24px" />
    <el-form
      ref="addForm"
      :rules="rules"
      :model="formData"
      class="form"
      size="small"
      label-position="top"
      label-width="110px">
      <el-form-item label="微博账号主页链接" prop="link">
        <el-input v-model="formData.link" clearable placeholder="请输入链接" />
      </el-form-item>
    </el-form>
    <div class="add-text">
      <div>注意：账号添加后系统会自动采集。输入的链接不可修改。</div>
      <div>例：https://weibo.com/u/1644395354</div>
    </div>
    <div class="center">
      <el-button
        :disabled="loading"
        :loading="loading"
        type="primary"
        size="small"
        @click="handleSubmit"
      >
        确定
      </el-button>
      <el-button
        type="info"
        size="small"
        class="cancel-btn-style"
        @click="handleClose"
      >取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Title from '@/components/Title';
import { mapState } from 'vuex';
export default {
  components: {
    Title
  },
  data() {
    return {
      formData: {
        link: ''
      },
      visible: false,
      loading: false
    };
  },
  computed: {
    rules() {
      const validateAddress = (_, value, callback) => {
        const isWeibo = /https:\/\/weibo.com\/[^\s]+/;
        if (!isWeibo.test(value)) {
          callback(new Error('请填写正确的微博链接'));
        }
        callback();
      };
      return {
        'link': [{ required: true, trigger: 'blur', validator: validateAddress }]
      };
    },
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    cancelLoading() {
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    handleClose() {
      this.visible = false;
      this.cancelLoading();
      this.$refs['addForm'].resetFields();
    },
    handleSubmit() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          const { link } = this.formData;
          const params = { link };
          this.loading = true;
          this.$emit('submit', params);
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.add-dialog {
  .form {
    margin-top: -6px;
  }
  &::v-deep {
    .el-form-item__label {
      color: #666;
    }
  }
  .add-text {
    color: #999;
    font-size: 12px;
    margin-bottom: 16px;
  }
  .center{
    display: flex;
    justify-content: center;
  }
}
</style>
